package com.luck.pf.util;

import java.util.List;

import com.luck.pf.common.Constants;
import com.luck.pf.common.entity.CodeNameVO;

public class HtmlTagUtils {
	private static HtmlTagUtils instance;

	public static HtmlTagUtils getInstance() {
		if (instance == null) {
			instance = new HtmlTagUtils();
		}
		return instance;
	}

	/**
	 * 生成选择复选选择框
	 * 
	 * @return
	 */
	/*
	 * public String generateCheckBox(String name,String id,""){ StringBuffer
	 * buf = new StringBuffer(); for(int i = 0,len = param.length;i<len;i++){
	 * buf.append("<input type=\"checkbox\" name=\""); buf.append(name);
	 * buf.append("\" id=\""); buf.append(id); buf.append(param[i].getCode());
	 * buf.append("\" value=\""); buf.append(param[i].getCode());
	 * buf.append("\"/> ");
	 * 
	 * buf.append(param[i].getName()); buf.append("&nbsp;&nbsp;"); } return
	 * buf.toString(); }
	 */
	/**
	 * 生成选择下拉框
	 * 
	 * @return
	 */
	public String generateSelect(String selectStr, String defaultName,
			String paramType) {
		List<CodeNameVO> codes = (List<CodeNameVO>) Constants.params.get(paramType);
		StringBuffer buf = new StringBuffer(selectStr);

		buf.append("<option value=\"\">");
		buf.append(defaultName);
		buf.append("</option>");

		for (CodeNameVO next : codes) {
			buf.append("<option value=\"");
			buf.append(next.getCode());
			buf.append("\">");
			buf.append(next.getName());
			buf.append("</option>");
		}

		buf.append("</select>");
		return buf.toString();
	}

	/**
	 * 生成特定选择下拉框
	 * 
	 * @return
	 */
	public String generateSelect(String selectStr, String defaultName,
			String paramType, String[] names) {
		List<CodeNameVO> codes = (List<CodeNameVO>) Constants.params.get(paramType);
		StringBuffer buf = new StringBuffer(selectStr);

		buf.append("<option value=\"\">");
		buf.append(defaultName);
		buf.append("</option>");

		for (CodeNameVO next : codes) {
			if (isIn(next.getCode(), names)) {
				buf.append("<option value=\"");
				buf.append(next.getCode());
				buf.append("\">");
				buf.append(next.getName());
				buf.append("</option>");
			}
		}

		buf.append("</select>");
		return buf.toString();
	}

	/**
	 * 判断给定的字符串是否在给定的字符串数组里面
	 */
	public boolean isIn(String value, String[] values) {
		boolean result = false;
		for (int i = 0; i < values.length; i++) {
			if (value.equals(values[i])) {
				result = true;
				break;
			}
		}
		return result;
	}
}
